package com.lds.erp.order.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dph.common.utils.utils.CommonUtils;
import com.lds.erp.base.baseset.GiftFacade;
import com.lds.erp.base.baseset.order.GiftCardGrabOrder;
import com.lds.erp.base.baseset.order.GiftCardQueryOrder;
import com.lds.erp.base.baseset.result.GiftCardGrabResult;
import com.lds.erp.base.baseset.result.GiftCardQueryResult;
import com.lds.erp.order.OrderCouponClient;
import com.lds.erp.order.coupon.OrderCouponFacade;
import com.lds.erp.order.coupon.info.OrderCouponMchDetailInfo;
import com.lds.erp.order.coupon.order.MyCouponOrder;
import com.lds.erp.order.coupon.result.MyCouponResult;
import com.lds.erp.order.enums.CouponMakeTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by liuli on 2019/1/8.
 */
@Service
public class OrderCouponClientImpl implements OrderCouponClient {
    private final static Logger logger = LoggerFactory.getLogger(OrderCouponClientImpl.class);
    @Reference(version = "1.0")
    private OrderCouponFacade orderCouponFacade;

    @Reference(version = "1.0")
    private GiftFacade giftFacade;


    @Override
    public MyCouponResult myCoupon(MyCouponOrder order) {
        logger.info("我的优惠券,入参={}", order);
        MyCouponResult result = orderCouponFacade.myCoupon(order);
        logger.info("我的优惠券,出参={}", result);
        return result;
    }

    @Override
    public MyCouponResult myCouponShow(MyCouponOrder order) {
        logger.info("我的优惠券,入参={}", order);
        MyCouponResult result = orderCouponFacade.myCoupon(order);
        logger.info("我的优惠券,出参={}", result);
        List<OrderCouponMchDetailInfo> all = new ArrayList<>();
        List<OrderCouponMchDetailInfo> newPerson = new ArrayList<>();
        List<OrderCouponMchDetailInfo> other = new ArrayList<>();
        // 单独处理点逻辑
        if (CommonUtils.isNotEmpty(result.getPageInfo()) && CommonUtils.isNotEmpty(result.getPageInfo().getList())) {
            for (OrderCouponMchDetailInfo temp : result.getPageInfo().getList()) {
                if (temp.getMakeType().equals(CouponMakeTypeEnum.FULLREDUCTION)) {
                    if (temp.getName().indexOf("新人券") >= 0) {
                        newPerson.add(temp);
                    } else {
                        other.add(temp);
                    }
                }

            }
            if (CommonUtils.isNotEmpty(newPerson)) {
                // 满足排序
                newPerson = newPerson.stream()
                        .sorted(Comparator.comparing(OrderCouponMchDetailInfo::getSatisfyAmount))
                        .collect(Collectors.toList());
                all.addAll(newPerson);
            }

            if (CommonUtils.isNotEmpty(other)) {
                // 满足排序
                other = other.stream()
                        .sorted(Comparator.comparing(OrderCouponMchDetailInfo::getSatisfyAmount))
                        .collect(Collectors.toList());
                all.addAll(other);
            }
            result.getPageInfo().setList(all);
        }
        logger.info("我的优惠券,处理后参数={}", result);
        return result;
    }
    @Override
    public GiftCardQueryResult giftCardQuery(GiftCardQueryOrder order) {
        logger.info("查询红包信息,入参={}", order);
        GiftCardQueryResult result = giftFacade.giftCardQuery(order);
        logger.info("查询红包信息,出参={}", result);
        return result;
    }

    @Override
    public GiftCardGrabResult giftCardGrab(GiftCardGrabOrder order) {
        logger.info("抢红包,入参={}", order);
        GiftCardGrabResult result = giftFacade.giftCardGrab(order);
        logger.info("抢红包,出参={}", result);
        return result;
    }
}
